﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;

namespace NotesCommonProjects
{
    public partial class LogTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string log_dir = NotesCF.StationeryLogDir;
            string context = "test test see ";


            //檢查Log根目錄
            DirectoryInfo di = new DirectoryInfo(log_dir);
            if (!di.Exists)
                di.Create();

            //檢查Log檔案
            string FileName = DateTime.Now.ToString("yyyy_MM_dd") + ".txt";
            /*
            FileInfo fi = new FileInfo(log_dir + FileName);
            if (!fi.Exists)
                fi.Create();*/

            //將context寫入檔案
            //FileStream LogFile = File.Open(log_dir + FileName, FileMode.Append, FileAccess.Write);
            FileStream fs = new FileStream(log_dir + FileName, FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(DateTime.Now.ToString("HH:mm:ss") + " : " + context + "\r\n\r\n");

            //釋放資源
            sw.Dispose();
            fs.Dispose();            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string log_dir = NotesCF.StationeryLogDir;
            string FileName = DateTime.Now.ToString("yyyy_MM_dd") + ".txt";
            string user_name_from_robot = "周正欣";

            //1. 用user_name_from_robot到log檔中找出 使用者的最新一筆log中的 Notes文件處理時間
            FileStream fs = new FileStream(log_dir + FileName, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            string LogContext = sr.ReadToEnd();

            int LastNameIndex = LogContext.LastIndexOf(user_name_from_robot);
            LogContext = LogContext.Substring(LastNameIndex);

            int NotesProcessedTimeIndex = LogContext.IndexOf("Notes文件處理時間：") + "Notes文件處理時間：".Length;
            string NotesProcessedTime = LogContext.Substring(NotesProcessedTimeIndex, LogContext.LastIndexOf("】") - NotesProcessedTimeIndex);

            //2. 用user_name_from_robot和Notes文件處理時間到 Stationery.NSF中找符合的文件 刪掉
            
        }
    }
}